<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="formData.projectName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="项目编号（唯一）" prop="projectCode">
        <el-input v-model="formData.projectCode" placeholder="请输入项目编号（唯一）" />
      </el-form-item>
      <el-form-item label="项目开始时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          type="date"
          value-format="x"
          placeholder="选择项目开始时间"
        />
      </el-form-item>
      <el-form-item label="项目结束时间" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          type="date"
          value-format="x"
          placeholder="选择项目结束时间"
        />
      </el-form-item>
      <el-form-item label="项目使用的币种" prop="currency">
        <el-input v-model="formData.currency" placeholder="请输入项目使用的币种" />
      </el-form-item>
      <el-form-item label="展览时间" prop="exhibitionTime">
        <el-date-picker
          v-model="formData.exhibitionTime"
          type="date"
          value-format="x"
          placeholder="选择展览时间"
        />
      </el-form-item>
      <el-form-item label="布展时间" prop="setupTime">
        <el-date-picker
          v-model="formData.setupTime"
          type="date"
          value-format="x"
          placeholder="选择布展时间"
        />
      </el-form-item>
      <el-form-item label="展览地点" prop="location">
        <el-input v-model="formData.location" placeholder="请输入展览地点" />
      </el-form-item>
      <el-form-item label="现场工程完成时间" prop="completionTime">
        <el-date-picker
          v-model="formData.completionTime"
          type="date"
          value-format="x"
          placeholder="选择现场工程完成时间"
        />
      </el-form-item>
      <el-form-item label="撤展时间" prop="dismantleTime">
        <el-date-picker
          v-model="formData.dismantleTime"
          type="date"
          value-format="x"
          placeholder="选择撤展时间"
        />
      </el-form-item>
      <el-form-item label="项目品类" prop="category">
        <el-input v-model="formData.category" placeholder="请输入项目品类" />
      </el-form-item>
      <el-form-item label="展台面积 (单位：平方米)" prop="boothArea">
        <el-input v-model="formData.boothArea" placeholder="请输入展台面积 (单位：平方米)" />
      </el-form-item>
      <el-form-item label="项目经理" prop="projectManager">
        <el-input v-model="formData.projectManager" placeholder="请输入项目经理" />
      </el-form-item>
      <el-form-item label="客户经理" prop="customerManager">
        <el-input v-model="formData.customerManager" placeholder="请输入客户经理" />
      </el-form-item>
      <el-form-item label="备注信息" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注信息" />
      </el-form-item>
      <el-form-item label="文件id" prop="fileIds">
        <el-input v-model="formData.fileIds" placeholder="请输入文件id" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ProjectsManageApi, ProjectsManageVO } from '@/api/shuhu/projectsmanage'

/** 项目管理 表单 */
defineOptions({ name: 'ProjectsManageForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  projectName: undefined,
  projectCode: undefined,
  startTime: undefined,
  endTime: undefined,
  currency: undefined,
  exhibitionTime: undefined,
  setupTime: undefined,
  location: undefined,
  completionTime: undefined,
  dismantleTime: undefined,
  category: undefined,
  boothArea: undefined,
  projectManager: undefined,
  customerManager: undefined,
  remark: undefined,
  fileIds: undefined
})
const formRules = reactive({
  projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
  projectCode: [{ required: true, message: '项目编号（唯一）不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProjectsManageApi.getProjectsManage(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProjectsManageVO
    if (formType.value === 'create') {
      await ProjectsManageApi.createProjectsManage(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProjectsManageApi.updateProjectsManage(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    projectName: undefined,
    projectCode: undefined,
    startTime: undefined,
    endTime: undefined,
    currency: undefined,
    exhibitionTime: undefined,
    setupTime: undefined,
    location: undefined,
    completionTime: undefined,
    dismantleTime: undefined,
    category: undefined,
    boothArea: undefined,
    projectManager: undefined,
    customerManager: undefined,
    remark: undefined,
    fileIds: undefined
  }
  formRef.value?.resetFields()
}
</script>